package com.example.demo3.others;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/Param")
public class ParamController {
    @RequestMapping("/p1")
    public String v1(String name){
        return "接收到参数name:" + name;
    }

    @RequestMapping("p2")
    public String v2(Integer num1){
        return "接收到参数num1" + num1;
    }

    @RequestMapping("p3")
    public String v3(int num2){
        return "接收到参数num2" + num2;
    }
    @RequestMapping("p4")
    public String v4(boolean flag){
        return "接收到参数flag" + flag;
    }
    //传递单个参数时,尽量使用包装类型,
    // 除了boolean类型外,其他的如果使用基本类型,没有参数时候会报500的错误,
    // 如果使用包装类型就会返回null,不会报错

    @RequestMapping("p5")
    public String v5(String name, Integer num){
        return "接收到参数name: "+ name + "num: " + num;
    }

    @RequestMapping("p6")
    public String v6(@RequestParam(value = "userName", required = false) String name,@RequestParam("userAge") Integer age){
        return "接收到参数userName: " + name + "userAge" + age;
    }

    @RequestMapping("/p7")
    public String v7(User user){
        return "接收到参数user: "+ user.toString();
    }
    @RequestMapping("/p8")
    public String v8(String[] arr){
        return "接收到参数arr: " + List.of(arr);
    }

    @RequestMapping("/p9")
    public String v9(@RequestParam List<String> list){//默认传来的是数组,要用@RequestParam绑定位list
        return "list" + list;
    }

    @RequestMapping("/p10")
    public String v10(@RequestBody User user){//要在请求正文中传递json数据,
        return "user: " + user;
    }

    //获取url中的数据
    @RequestMapping("/p11/{id}/{userName}")
    public String v11(@PathVariable Integer id, @PathVariable("userName") String name){
        return "获取到url中的参数 , id : " + id + ", userName : " + name;
    }

    //上传图片
    @RequestMapping("/p12")
    public String v12(@RequestPart("fileName") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        file.transferTo(new File("D:/temp/" +fileName));
        return fileName;
    }

    //获取cookie
    @RequestMapping("/p13")
    public String v13(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if(cookies == null){
            return null;
        }
        for(Cookie cookie : cookies){
            System.out.println(cookie.getName() + ": " + cookie.getValue());
        }
        return "获取cookie成功";
    }
    //通过注解获取cookie
    @RequestMapping("/p14")
    public String v14(@CookieValue("qiuzhao") String qiuzhao){
        return "获取cookie信息,秋招: "+ qiuzhao;
    }

    //获取session
    @RequestMapping("p15")
    public String v15(HttpServletRequest request){
        HttpSession session = request.getSession();
        if(session == null){
            return null;
        }
        String userName = (String)session.getAttribute("userName");
        System.out.println(session.getAttribute("age"));
        System.out.println(session.getAttribute("userName"));
        return "从session中获取userName: " + userName;
    }
    //设置session
    @RequestMapping("/p16")
    public String v16(HttpServletRequest request){
        HttpSession session = request.getSession();
        if(session == null){
            return null;
        }
        session.setAttribute("userName", "zhangyi");

        session.setAttribute("age","21");

        return "设置session成功!";

    }

    @RequestMapping("/p17")
    public String v17(HttpSession session){
        System.out.println(session.getAttribute("userName"));;
        System.out.println(session.getAttribute("age"));
        return "getSession成功: " ;
    }

    @RequestMapping("/p18")
    public String v18(@SessionAttribute("userName") String name){
        return "获取到session成功,userName: " + name;
    }

    //获取header
    @RequestMapping("/p19")
    public String v19(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return "获取header中的User-Agent成功: "+ userAgent;
    }

    @RequestMapping("/p20")
    public String v20(@RequestHeader("User-Agent") String userAgent){
        return "获取userAgent: "+userAgent;
    }
}

